﻿using System;
using System.Collections.Generic;
using System.Linq;
using GalaSoft.MvvmLight.Messaging;
using LiSAClient.Core.Messages;
using LiSAClient.Core.Services;
using LiSAClient.Core.Services.Factories;
using LiSAClient.Core.Viewmodel;
using LiSAClient.LiSAService;

namespace LiSAClient.WpfClient.ViewModel.Projects.ArticleFeedback
{
    public class FeedbackStageViewModel : ContentViewModel
    {
        #region Observable Objects
        public ProjectStage Stage { get; set; }

        public LiSAService.ArticleFeedback TheFeedback { get; private set; }

        private string description;
        public string Description
        {
            get { return this.description; }

            set { this.Set(() => this.Description, ref this.description, value); }
        }

        private List<FeedbackItemViewModel> items;
        public List<FeedbackItemViewModel> Items
        {
            get { return this.items; }

            set { this.Set(() => this.Items, ref this.items, value); }
        }
        #endregion

        #region Initializers
        /// <summary>
        /// Initializes empty viewmodel
        /// </summary>
        public FeedbackStageViewModel()
        {
            this.Items = new List<FeedbackItemViewModel>();
            this.TheFeedback = new LiSAService.ArticleFeedback();
            this.Stage = new ProjectStage();
        }

        /// <summary>
        /// Initializes a viewmodel with existing ArticleFeedback
        /// </summary>
        /// <param name="stage">All Stages for the Project</param>
        /// <param name="feedback">All feedback for the article</param>
        public FeedbackStageViewModel(ProjectStage stage, LiSAService.ArticleFeedback feedback)
        {
            this.Description = stage.Description;
            // Initialize all the phases
            this.Items = new List<FeedbackItemViewModel>();
            this.TheFeedback = feedback;
            this.Stage = stage;
            var feedbackID = Guid.Empty;

            foreach (var decisionPoint in stage.DecisionPoints)
            {
                var isChecked = false;
                if (TheFeedback.OptionSelected != null)
                {
                     isChecked = String.Equals(decisionPoint.Trim(), TheFeedback.OptionSelected.Trim(),
                        StringComparison.CurrentCultureIgnoreCase);
                }
                this.Items.Add(new FeedbackItemViewModel(decisionPoint, isChecked, feedbackID));
            }

            // Listen to them all
            foreach (var feedbackItemViewModel in Items)
            {
                feedbackItemViewModel.IsCheckedChangeEventHandler += FeedBackItemCheckedChanged;
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Event to fire whenever an option is checked
        /// </summary>
        /// <param name="sender">The FeedbackItemViewModel that has changed </param>
        /// <param name="eventArgs">The EventArgs</param>
        private void FeedBackItemCheckedChanged(object sender, EventArgs eventArgs)
        {
            var selected = sender as FeedbackItemViewModel;
            if (selected == null || Items == null)
            {
                return;
            }

            //uncheck remaining options
            foreach (var feedbackItemViewModel in Items.Where(x => x.DecisionText != selected.DecisionText))
            {
                feedbackItemViewModel.UnCheck();
            }

            //save the change
            this.TheFeedback.OptionSelected = selected.DecisionText;
            ServiceFactory.Instance.Get<ProjectService>().SaveOrUpdateArticleFeedback(this.TheFeedback);
            Messenger.Default.Send(new ArticleFeedbackChangedMessage(this.TheFeedback, this.Stage));
        }
        #endregion
    }
}
